﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using AssignmentsTableAdapters;

/// <summary>
/// Summary description for AssignmentsSubmissionsBLL
/// </summary>
[System.ComponentModel.DataObject]
public class AssignmentsSubmissionsBLL
{
    ILE_Global ileGlobal = new ILE_Global();

    #region Adapters
    private Assignments_Submission_QuizTableAdapter _quizSubmissionAdapter = null;
    protected Assignments_Submission_QuizTableAdapter QuizSubmissionAdapter
    {
        get
        {
            if (_quizSubmissionAdapter == null)
                _quizSubmissionAdapter = new Assignments_Submission_QuizTableAdapter();

            return _quizSubmissionAdapter;
        }
    }

    private Assignments_Submission_EssayTableAdapter _essaySubmissionAdapter = null;
    protected Assignments_Submission_EssayTableAdapter EssaySubmissionAdapter
    {
        get
        {
            if (_essaySubmissionAdapter == null)
                _essaySubmissionAdapter = new Assignments_Submission_EssayTableAdapter();

            return _essaySubmissionAdapter;
        }
    }

    private Assignments_Submission_Essay_CommentsTableAdapter _essaySubmissionCommentsAdapter = null;
    protected Assignments_Submission_Essay_CommentsTableAdapter EssaySubmissionCommentsAdapter
    {
        get
        {
            if (_essaySubmissionCommentsAdapter == null)
                _essaySubmissionCommentsAdapter = new Assignments_Submission_Essay_CommentsTableAdapter();

            return _essaySubmissionCommentsAdapter;
        }
    }

    #endregion

    #region Quiz
    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, true)]
    public Assignments.Assignments_Submission_QuizDataTable GetAllQuizSubmissions()
    {
        return QuizSubmissionAdapter.GetQuizSubmissions();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_Submission_QuizDataTable GetQuizSubmissionsByAssignmentIdAndUserId(int AssignmentID, int UserID)
    {
        return QuizSubmissionAdapter.GetQuizSubmissionsByAssignmentIdAndUserId(AssignmentID, UserID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_Submission_QuizDataTable GetQuizSubmissionsByAssignmentIdAndCourseIdAndSectionNumber(int AssignmentID, int CourseID, int SectionNumber)
    {
        return QuizSubmissionAdapter.GetQuizSubmissionsByAssignmentIdAndCourseIdAndSectionNumber(AssignmentID, CourseID, SectionNumber);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_Submission_QuizDataTable GetQuizSubmissionsByAssignmentIdAndCourseIdAndSectionNumberAndUserID(int AssignmentID, int CourseID, int SectionNumber, int UserID)
    {
        return QuizSubmissionAdapter.GetQuizSubmissionsByAssignmentIdAndCourseIdAndSectionNumberAndUserID(AssignmentID, CourseID, SectionNumber, UserID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_Submission_QuizDataTable GetQuizSubmissionsByAssignmentIdAndCourseIdAndSectionNumberAndFilter(int AssignmentID, int CourseID, int SectionNumber, string Filter)
    {
        Assignments.Assignments_Submission_QuizDataTable results =
            QuizSubmissionAdapter.GetQuizSubmissionsByAssignmentIdAndCourseIdAndSectionNumber(AssignmentID, CourseID, SectionNumber);


        if (results.Count == 0 || Filter == "All")
        {
            return results;
        }
        else
        {
            Assignments.Assignments_Submission_QuizDataTable filteredResults = new Assignments.Assignments_Submission_QuizDataTable();
            Assignments.Assignments_Submission_QuizDataTable tempFilteredResults = new Assignments.Assignments_Submission_QuizDataTable();

            int userID = results[0].UserID;
            foreach (Assignments.Assignments_Submission_QuizRow row in results)
            {
                if (userID == row.UserID)
                {
                    tempFilteredResults.ImportRow(row);
                }
                else
                {
                    filteredResults.ImportRow(tempFilteredResults[0]);
                    foreach (Assignments.Assignments_Submission_QuizRow tempRow in tempFilteredResults)
                    {
                        if (Filter == "Highest")
                        {
                            if (tempRow.GradePercentage > filteredResults[filteredResults.Count - 1].GradePercentage)
                            {
                                filteredResults.Rows.RemoveAt(filteredResults.Count - 1);
                                filteredResults.ImportRow(tempRow);
                            }
                        }
                        else
                        {
                            if (tempRow.GradePercentage < filteredResults[filteredResults.Count - 1].GradePercentage)
                            {
                                filteredResults.Rows.RemoveAt(filteredResults.Count - 1);
                                filteredResults.ImportRow(tempRow);
                            }
                        }

                    }

                    tempFilteredResults.Clear();
                    tempFilteredResults.ImportRow(row);
                    userID = row.UserID;
                }
            }

            filteredResults.ImportRow(tempFilteredResults[0]);
            foreach (Assignments.Assignments_Submission_QuizRow tempRow in tempFilteredResults)
            {
                if (Filter == "Highest")
                {
                    if (tempRow.GradePercentage > filteredResults[filteredResults.Count - 1].GradePercentage)
                    {
                        filteredResults.Rows.RemoveAt(filteredResults.Count - 1);
                        filteredResults.ImportRow(tempRow);
                    }
                }
                else
                {
                    if (tempRow.GradePercentage < filteredResults[filteredResults.Count - 1].GradePercentage)
                    {
                        filteredResults.Rows.RemoveAt(filteredResults.Count - 1);
                        filteredResults.ImportRow(tempRow);
                    }
                }

            }
            tempFilteredResults.Clear();

            return filteredResults;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_Submission_QuizRow GetQuizSubmission(int QuizSubmissionID)
    {
        return QuizSubmissionAdapter.GetQuizSubmission(QuizSubmissionID)[0];
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public int InsertQuizSubmission(int AssignmentID, int UserID, int CourseID, int SectionNumber, int StandardID, string StudentAnswers, decimal Grade, DateTime StartedDateTime)
    {
        int Attempts = 1;
        Assignments.Assignments_Submission_QuizDataTable quizSubmissions = GetQuizSubmissionsByAssignmentIdAndUserId(AssignmentID, UserID);
        if (quizSubmissions.Count != 0)
        {
            Attempts = quizSubmissions[0].Attempts + 1;
        }

        return Convert.ToInt32(QuizSubmissionAdapter.InsertQuizSubmission(AssignmentID, UserID, CourseID, SectionNumber, StandardID,
            Attempts, StudentAnswers, Grade, StartedDateTime, DateTime.Now));
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateQuizSubmission(string StudentAnswers, decimal Grade, int QuizSubmissionID)
    {
        return QuizSubmissionAdapter.UpdateQuizSubmission(StudentAnswers, Grade, DateTime.Now, QuizSubmissionID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool MoveQuizSubmissionToOtherCourseSection(int CourseID, int SectionNumber, int QuizSubmissionID)
    {
        return QuizSubmissionAdapter.MoveQuizSubmissionToOtherCourseSection(CourseID, SectionNumber, QuizSubmissionID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Delete, false)]
    public bool DeleteQuizSubmission(int QuizSubmissionID)
    {
        return QuizSubmissionAdapter.DeleteQuizSubmissionByQuizSubmissionID(QuizSubmissionID) == 1;
    }

    #endregion

    #region Essay

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, true)]
    public Assignments.Assignments_Submission_EssayDataTable GetAllEssaySubmissions()
    {
        return EssaySubmissionAdapter.GetEssaySubmissions();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_Submission_EssayRow GetEssaySubmissionByAssignmentIdAndUserId(int AssignmentID, int UserID)
    {
        try
        {
            return EssaySubmissionAdapter.GetEssaySubmissionByAssignmentIdAndUserId(AssignmentID, UserID)[0];
        }
        catch
        {
            return null;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_Submission_EssayDataTable GetEssaySubmissionsByAssignmentIdAndCourseIdAndSectionNumberAndFilter(int AssignmentID, int CourseID, int SectionNumber, string Filter)
    {
        Assignments.Assignments_Submission_EssayDataTable results =
            EssaySubmissionAdapter.GetEssaySubmissionsByAssignmentIdAndCourseIdAndSectionNumber(AssignmentID, CourseID, SectionNumber);


        if (results.Count == 0 || Filter == "All")
        {
            return results;
        }
        else
        {
            Assignments.Assignments_Submission_EssayDataTable filteredResults = new Assignments.Assignments_Submission_EssayDataTable();

            foreach (Assignments.Assignments_Submission_EssayRow row in results)
            {
                if (Filter == "Graded")
                {
                    if (row.Grade.Trim() != String.Empty)
                    {
                        filteredResults.ImportRow(row);
                    }
                }
                else
                {
                    if (row.Grade.Trim() == String.Empty)
                    {
                        filteredResults.ImportRow(row);
                    }
                }
            }

            return filteredResults;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_Submission_EssayRow GetEssaySubmission(int EssaySubmissionID)
    {
        return EssaySubmissionAdapter.GetEssaySubmission(EssaySubmissionID)[0];
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public int InsertEssaySubmission(int AssignmentID, int UserID, int CourseID, int SectionNumber, int StandardID, string StudentAnswers, bool IsFinal, DateTime SubmittedDateTime)
    {
        return Convert.ToInt32(EssaySubmissionAdapter.InsertEssaySubmission(AssignmentID, UserID, CourseID, SectionNumber, StandardID, StudentAnswers, IsFinal, String.Empty, DateTime.Now, SubmittedDateTime));
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateEssaySubmission(string StudentAnswers, bool IsFinal, int EssaySubmissionID)
    {
        return EssaySubmissionAdapter.UpdateEssaySubmission(StudentAnswers, IsFinal, DateTime.Now, EssaySubmissionID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool MoveEssaySubmissionToOtherCourseSection(int CourseID, int SectionNumber, int EssaySubmissionID)
    {
        return EssaySubmissionAdapter.MoveEssaySubmissionToOtherCourseSection(CourseID, SectionNumber, EssaySubmissionID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateEssaySubmissionIsFinal(bool IsFinal, int EssaySubmissionID)
    {
        return EssaySubmissionAdapter.UpdateEssaySubmissionIsFinal(IsFinal, EssaySubmissionID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateEssaySubmissionGrade(string Grade, int EssaySubmissionID)
    {
        return EssaySubmissionAdapter.UpdateEssaySubmissionGrade(Grade, EssaySubmissionID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Delete, false)]
    public bool DeleteEssaySubmission(int EssaySubmissionID)
    {
        return EssaySubmissionAdapter.DeleteEssaySubmissionByEssaySubmissionID(EssaySubmissionID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_Submission_Essay_CommentsDataTable GetEssaySubmissionCommentsByEssaySubmissionID(int EssaySubmissionID)
    {
        return EssaySubmissionCommentsAdapter.GetEssaySubmissionCommentsByEssaySubmission(EssaySubmissionID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_Submission_Essay_CommentsRow GetEssaySubmissionComment(int EssaySubmissionID, int UserID)
    {
        return EssaySubmissionCommentsAdapter.GetEssaySubmissionComment(EssaySubmissionID, UserID)[0];
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public bool InsertEssaySubmissionComment(int EssaySubmissionID, int UserID, string Comment)
    {
        return EssaySubmissionCommentsAdapter.Insert(EssaySubmissionID, UserID, Comment, DateTime.Now) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateEssaySubmissionComment(string Comment, int EssaySubmissionID, int UserID)
    {
        return EssaySubmissionCommentsAdapter.Update(Comment, DateTime.Now, EssaySubmissionID, UserID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public bool IsEssaySubmissionCommentDuplicated(int EssaySubmissionID, int UserID)
    {
        return EssaySubmissionCommentsAdapter.IsDuplicated(EssaySubmissionID, UserID) == 1;
    }


    #endregion

}
